//
//  MultiSegmentController.m
//  MultiSegmentController
//
//  Created by 郑林琴 on 15/5/24.
//  Copyright (c) 2015年 Binglin. All rights reserved.
//

#import "MultiSegmentController.h"
#import "BIMultiSegmentControl.h"



@interface MultiSegmentController ()<SegmentSelectProtocol, UIScrollViewDelegate>

@property (nonatomic, strong) BIMultiSegmentControl *segmentControl;
@property (nonatomic, strong) UIViewController *selectedViewController;
@property (nonatomic, strong) NSArray<UIViewController *> *viewControllers;
@property (nonatomic, strong) UIScrollView  *scrollContainer;

@end

@implementation MultiSegmentController

- (instancetype)initWithSegmentControl:(BIMultiSegmentControl *)control viewControllers:(NSArray<UIViewController *> *)viewControllers{
    if (self = [super init]) {
        self.segmentControl = control;
        self.segmentControl.delegate = self;
        self.viewControllers = viewControllers;
    }
    return self;
}

- (UIScrollView *)scrollContainer{
    if (!_scrollContainer) {
        _scrollContainer = [[UIScrollView alloc] initWithFrame:({CGRect frame = self.view.frame; frame.origin.y = CGRectGetHeight(self.segmentControl.frame);frame.size.height -= CGRectGetHeight(self.segmentControl.frame); frame;})];
        _scrollContainer.delegate = self;
        _scrollContainer.scrollsToTop = NO;
        _scrollContainer.pagingEnabled = YES;
        _scrollContainer.bounces = NO;
        _scrollContainer.contentSize = CGSizeMake(self.view.frame.size.width * self.viewControllers.count, CGRectGetHeight(self.view.frame));
        _scrollContainer.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_scrollContainer];
        _scrollContainer.backgroundColor = [UIColor whiteColor];
        
    }
    return _scrollContainer;
}

- (void)loadView{
    [super loadView];
    [self.view addSubview:self.scrollContainer];
    [self.view addSubview:self.segmentControl];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

//- (void)dealloc{
//    [_scrollContainer.panGestureRecognizer ]
//}

- (void)viewDidLoad{
    [super viewDidLoad];
    self.segmentControl.selectIndex = self.selectIndex;
}

#pragma mark - private
- (void)showViewControllerAtIndex:(NSInteger)index{
    if (index < 0 || index >= self.viewControllers.count) {
        return;
    }
    
    UIViewController *indexVC  = self.viewControllers[index];
    
    if (indexVC.parentViewController == nil) {
        [self addChildViewController:indexVC];
        [_scrollContainer addSubview:indexVC.view];
        
        CGFloat heightDecrement = CGRectGetHeight(self.segmentControl.frame);
        if (self.segmentLocation == MultiSegmentLocationNone) {
            heightDecrement = 0;
        }
        indexVC.view.frame = CGRectMake( CGRectGetWidth(self.view.frame) * index, 0 , CGRectGetWidth(self.view.frame) ,CGRectGetHeight(self.view.frame) - heightDecrement);
    }
}

#pragma mark - SelectViewDelegate

- (void)segmentControlDidSelectIndex:(NSInteger)tag{
    
    _selectIndex = tag ;
    
    [self.selectedViewController removeFromParentViewController];
    [self.selectedViewController.view removeFromSuperview];
    self.selectedViewController = self.viewControllers[tag];
    [self showViewControllerAtIndex:tag];
    [self.delegate segmentControlDidSelectIndex:tag];
    _scrollContainer.contentOffset = CGPointMake(tag * CGRectGetWidth(self.view.frame), 0);
    
}

- (void)setSelectIndex:(NSUInteger)index{
    [self segmentControlDidSelectIndex:index];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger floor = floorf(scrollView.contentOffset.x / scrollView.frame.size.width);
    NSInteger ceil  = ceilf(scrollView.contentOffset.x / scrollView.frame.size.width);
    
    [self showViewControllerAtIndex:floor];
    [self showViewControllerAtIndex:ceil];
    
    [self.segmentControl setIndicatorProgress:scrollView.contentOffset.x / (scrollView.contentSize.width - CGRectGetWidth(self.segmentControl.frame))];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger floor = floorf(scrollView.contentOffset.x / scrollView.frame.size.width);
    NSLog(@"aaa end %ld",(long)floor);
    
    NSInteger index = floor >= self.viewControllers.count ? self.viewControllers.count - 1 : floor;
    
    if (index == self.segmentControl.selectIndex) {
        return;
    }
    self.segmentControl.selectIndex = index;
}


#pragma mark - override
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    ///segmentControl在顶部
    if (self.segmentLocation == MultiSegmentLocationTop) {
        
        self.segmentControl.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.segmentControl.frame));
        
        _scrollContainer.frame = ({CGRect frame = self.view.bounds;
            frame.origin.y = CGRectGetHeight(self.segmentControl.frame);
            frame.size.height -= CGRectGetHeight(self.segmentControl.frame);
            frame;
        });
        _scrollContainer.contentSize = CGSizeMake(self.view.frame.size.width * self.viewControllers.count, CGRectGetHeight(self.view.frame) - self.segmentControl.frame.size.height);
    }
    
    // 底部 可当tabbarController
    else if (self.segmentLocation == MultiSegmentLocationBottom){
        self.segmentControl.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.segmentControl.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.segmentControl.frame));
                
        _scrollContainer.frame = ({CGRect frame = self.view.bounds;
            frame.size.height -= CGRectGetHeight(self.segmentControl.frame);
            frame;
        });
        _scrollContainer.contentSize = CGSizeMake(self.view.frame.size.width * self.viewControllers.count, CGRectGetHeight(self.view.bounds) - self.segmentControl.frame.size.height);
    }
    
    ////不加在MultiSegmentController界面中适用
    else if (self.segmentLocation == MultiSegmentLocationNone){
        _scrollContainer.frame = self.view.bounds;
           
        _scrollContainer.contentSize = CGSizeMake(self.view.frame.size.width * self.viewControllers.count, self.view.bounds.size.height);
    }
    
    [self segmentControlDidSelectIndex:self.segmentControl.selectIndex];
}

@end
